package com.zcpt.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zcpt.pojo.Sponsor;
import com.zcpt.service.SponsorService;

@Controller
@RequestMapping("/sponsor")
public class SponsorController {
	@Resource
	private SponsorService sponsorService;

	// 返回id的发起者信息
	@RequestMapping(value = "/{id}/spon", method = RequestMethod.GET)
	public String getSponsorById(@PathVariable("id") Integer sponsorId, Model model) {
		if (sponsorId == null) {
			return "redirect:/sponsor/error";// id不存在错误
		}
		Sponsor sponsor = sponsorService.getSponsorById(sponsorId);
		if (sponsor == null) {
			return "redirect:/sponsor/error";// spon不存在错误
		}
		model.addAttribute("spon", sponsor);
		return "spon";
	}

	// 返回所有发起者信息
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String getAllSponsor(Model model) {
		List<Sponsor> spoList = sponsorService.getAllSponsor();
		model.addAttribute("list", spoList);
		return "spon_list";
	}

	// 返回id的发起者和对应项目信息
	@RequestMapping(value = "/{id}/spon_pro", method = RequestMethod.GET)
	public String getSponsorByIdWithProject(@PathVariable("id") Integer sponsorId, Model model) {
		if (sponsorId == null) {
			return "redirect:/sponsor/error";// id不存在错误
		}
		Sponsor spo = sponsorService.getSponsorByIdWithProject(sponsorId);
		if (spo == null) {
			return "redirect:/sponsor/error";// spon不存在错误
		}
		model.addAttribute("spon_pro", spo);
		return "spon_pro";
	}

	// 添加发起者信息
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addSponosr(HttpServletRequest req) {
		try {
			req.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		String sponsorName = req.getParameter("sponName");
		String sponsorGender = req.getParameter("sponGender");
		String sponsorPhone = req.getParameter("sponPhone");
		Sponsor sponsor = new Sponsor();
		sponsor.setSponsorName(sponsorName);
		sponsor.setSponsorGender(sponsorGender);
		sponsor.setSponsorPhone(sponsorPhone);
		if (sponsorService.addSponsor(sponsor)) {
			return "add_success";// 添加成功页面
		} else {
			return "add_failure";// 添加失败页面
		}
	}

	// 修改发起者信息
	@RequestMapping(value = "/alter", method = RequestMethod.POST)
	public String alterSponsor(HttpServletRequest req) {
		try {
			req.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		int sponsorId = Integer.parseInt(req.getParameter("sponId"));
		Sponsor spo = sponsorService.getSponsorById(sponsorId);
		if (spo == null) {
			return "redirect:/sponsor/error";
		}
		String sponsorName = req.getParameter("sponName");
		String sponsorGender = req.getParameter("sponGender");
		String sponsorPhone = req.getParameter("sponPhone");
		Sponsor sponsor = new Sponsor();
		sponsor.setSponsorId(sponsorId);
		sponsor.setSponsorName(sponsorName);
		sponsor.setSponsorGender(sponsorGender);
		sponsor.setSponsorPhone(sponsorPhone);
		if (sponsorService.alterSponsor(sponsor)) {
			return "alter_success";// 修改成功页面
		} else {
			return "alter_failure";// 修改失败页面
		}
	}

	@RequestMapping(value = "/error")
	public String error() {
		return "error";
	}
}
